package com.gouaixin.bee.beeframework2.dao;

import android.content.Context;
import android.util.Log;


import com.google.gson.Gson;
import com.gouaixin.bee.beeframework2.bean.FANGKE;
import com.gouaixin.bee.beeframework2.tool.PreferencesUtils;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.db.sqlite.WhereBuilder;
import com.lidroid.xutils.exception.DbException;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/4/7.
 */
//访客对象数据库操作器
public class FangkeDao {

    public static void add(Context context, FANGKE bean){
        try {
            //DbUtils db = DbUtils.create(this.getActivity(), "/sdcard/", "test.db");
            DbUtils db = DbUtils.create(context);
            db.configAllowTransaction(true);
            db.configDebug(true);

            //首先判断数据库是否已经存在当前对象
            List<FANGKE> list =new ArrayList<FANGKE>();

            String jid=bean.getJid();
            Log.e("db","jid="+jid);

            list = db.findAll(Selector.from(FANGKE.class).where(WhereBuilder.b("jid", "=", jid)));

//                Log.e("db",new Gson().toJson(list));
//                Log.e("db","lsit.size()="+list.size());

                if(list==null||list.size()==0){
                    db.saveBindingId(bean);
                    String cid= PreferencesUtils.getString(context, "cid");
                    int fangkeNumer= PreferencesUtils.getInt(context, cid + "Fangke", 0);
                    fangkeNumer++;
                    PreferencesUtils.putInt(context, cid+"Fangke",fangkeNumer);
                }





        } catch (DbException e) {
            Log.e("db",e.getMessage());
        }
    }





    public static List<FANGKE> query(Context context){
        List<FANGKE> list =new ArrayList<FANGKE>();

        try {
            //DbUtils db = DbUtils.create(this.getActivity(), "/sdcard/", "test.db");
            DbUtils db = DbUtils.create(context);
            db.configAllowTransaction(true);
            db.configDebug(true);


             list = db.findAll(Selector.from(FANGKE.class));//.where(WhereBuilder.b("name", "=", "child' name")));
            Log.e("db",new Gson().toJson(list));

        } catch (DbException e) {
            Log.e("db",e.getMessage());
        }

        return list;
    }




}
